package cn.git.future;

import cn.git.entity.Person;

import java.util.concurrent.*;

/**
 * @description: 多任务组合处理
 * @program: bank-credit-sy
 * @author: lixuchun
 * @create: 2024-08-07
 */
public class CompletableFutureDemo11 {

    static ThreadPoolExecutor executor = new ThreadPoolExecutor(
            5,
            50,
            10,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(100),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 參數信息
        Person person = new Person();

        // 没有返回结果，串行执行任务
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            person.setAge(15);
            person.setName("jack");
            System.out.println("子线程1执行了：" + Thread.currentThread().getName());
            return 50;
        }, executor);

        Person person2 = new Person();
        // 串行执行任务
        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线程3执行了：" + Thread.currentThread().getName());
            person2.setName("tom");
            person2.setAge(18);
            return 100;
        }, executor).exceptionally((e) -> {
            // 线程3执行结果作为线程4入参参数
            System.out.println("子线程4执行了：" + Thread.currentThread().getName());
            System.out.println("子线程4执行异常了：" + e.getMessage());
            return -1;
        });

        Person person3 = new Person();

        // 串行执行任务
        CompletableFuture<Integer> future3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("子线程5执行了：" + Thread.currentThread().getName());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            person.setAge(13);
            return 200;
        }, executor).exceptionally((e) -> {
            // 线程3执行结果作为线程4入参参数
            System.out.println("子线程6执行了：" + Thread.currentThread().getName());
            System.out.println("子线程6执行异常了：" + e.getMessage());
            return -1;
        });

        // 等待三个任务执行完成 才开始进行后续处理操作,需要進行阻塞操作，还有anyOf任意任意一个任务执行完成即可
        final CompletableFuture<Void> finalFuture = CompletableFuture.allOf(future1, future2, future3);
        // 开始阻塞，等待三个方法执行完毕，也可使用join,join不需要自己处理异常，get需要自己处理
        finalFuture.get();

        // 获取全部结果
        final Integer integer1 = future1.get();
        final Integer integer2 = future2.get();
        final Integer integer3 = future3.get();

        System.out.println("线程1执行结果：" + integer1);
        System.out.println("线程2执行结果：" + integer2);
        System.out.println("线程3执行结果：" + integer3);

        System.out.println("三个任务全部执行完毕啦。。。。。。");
    }
}
